<template>
	<view class="index p10 c0">
		<view class="bf bs6 p10 f14">
			<u--form labelPosition="left" :rules="rules" ref="uForm" :borderBottom="false"
				:labelWidth="100" :labelStyle="{'font-size':'28rpx','color':'#000'}">
				<u-form-item label="所属区域" prop="areaId"  @click="showArea = true">
					<u--input v-model="form.area && form.area.name" border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="桌位类型" prop="typeId" @click="showType = true">
					<u--input v-model="form.type && form.type.name" disabled disabledColor="#ffffff" placeholder="请选择桌位类型"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="桌位编号" prop="name">
					<u--input v-model="form.name" border="none"></u--input>
				</u-form-item>
				<u-form-item label="桌位标识码">
					<u--input v-model="form.zjm" border="none"></u--input>
				</u-form-item>
			</u--form>
			<u-action-sheet :show="showArea" :actions="areaList" @close="showArea = false" @select="selectArea">
			</u-action-sheet>
			<u-action-sheet :show="showType" :actions="typeList" @close="showType = false" @select="selectType">
			</u-action-sheet>
		</view>
		<view class="mt10 cf5 f-x-c f14" @click="delet">
			<u-icon name="trash" color="#FD8906" size="20"></u-icon>删除
		</view>
		<view class="buts bf5">
			<view class="p-0-20 mb10">
				<view class="bs30 tac p10 f14 wei c0" :style="{background:subColor}" @click="save">保存</view>
			</view>
			<view class="c8 tac pb40 f14" @click="cancel">取消</view>
		</view>
		<!-- 模态框 -->
		<u-modal :show="show" title="删除" cancelText="取消" confirmColor="#FD8906" :showCancelButton="true" :closeOnClickOverlay="true"
			@close="show=false" @cancel="show=false" @confirm="confirm">
			<view class="tac" slot="default">您确认删除当前桌台吗?</view>
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				subColor: uni.getStorageSync('subject_color'),
				id: 0,
				show: false,
				showArea: false,
				showType: false,
				areaList: [],
				typeList: [],
				form: {},
				rules: {},
			}
		},
		onLoad(options) {
			if (options.id) {
				this.id = options.id
				this.getTableinf(options.id)
			}
			this.getTableArea()
			this.getTableType()
		},
		methods: {
			async getTableinf(id) {
				const {
					data
				} = await this.beg.request({
					url: `${this.api.tableList}/${id}`,
				})
				this.form = data ? data : {}
			},
			async getTableArea() {
				const {
					data: {
						list
					}
				} = await this.beg.request({
					url: this.api.tableArea,
					data: {
						pageSize: 999
					}
				})
				this.areaList = list ? list : []
			},
			async getTableType() {
				const {
					data: {
						list
					}
				} = await this.beg.request({
					url: this.api.tableType,
					data: {
						pageSize: 999
					}
				})
				this.typeList = list ? list : []
			},
			selectArea(e) {
				this.form.areaId = e.id
				this.form.area.name = e.name
			},
			selectType(e) {
				this.form.typeId = e.id
				this.form.type.name = e.name
			},
			delet() {
				this.show = true
			},
			async save() {
				const {
					msg
				} = await this.beg.request({
					url: `${this.api.tableList}/${this.id}`,
					method:'PUT',
					data:this.form
				})
				uni.$u.toast(msg)
				uni.reLaunch({
					url: "/pages/order/tabels/index"
				})
			},
			cancel() {
				uni.navigateBack({
					delta: 1
				});
			},
			async confirm() {
				const {
					msg
				} = await this.beg.request({
					url: `${this.api.tableList}/${this.id}`,
					method:'DELETE',
					data:this.form
				})
				uni.reLaunch({
					url: "/pages/order/tabels/index"
				})
				this.show = false
			},
		},
	}
</script>

<style lang="scss" scoped>
	/deep/.u-input__content__field-wrapper__field {
		text-align: right !important;
	}

	.buts {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
	}
</style>